import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:fps_monitor/util/collection_util.dart';
import 'package:fps_monitor/widget/custom_widget_inspector.dart';
import 'package:test_performance/ui/test1.dart';
import 'package:test_performance/ui/test2.dart';
import 'package:test_performance/ui/test3_isolate.dart';
import 'package:test_performance/ui/test4_provider.dart';
import 'package:test_performance/ui/test5_const_key.dart';
import 'package:test_performance/ui/test6_animation_controller.dart';
import 'package:test_performance/ui/test7_time_line.dart';

void main() {
  // //观测台里面显示build构建数
  // debugProfileBuildsEnabled = true;
  // //观测台里面显示Paints构建数
  // debugProfilePaintsEnabled = true;
  // //观测台里面显示Layout构建数
  // debugProfileLayoutsEnabled = true;

  runApp(const MyApp());
}

///fps 1
GlobalKey<NavigatorState> globalKey = GlobalKey();

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///fps 2
    kFpsInfoMaxSize = 1000;
    SchedulerBinding.instance?.addPostFrameCallback((timeStamp) {
      overlayState = globalKey.currentState?.overlay ?? OverlayState();
    });

    return MaterialApp(
      ///fps 3
      navigatorKey: globalKey,

      title: 'Flutter Demo',

      ///app显示性能统计数据
      // showPerformanceOverlay: true,

      ///gpu 检查屏幕之外的视图
      // checkerboardOffscreenLayers: true,

      ///cpu 图片缓存
      // checkerboardRasterCacheImages: true,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),

      ///fps 4
      builder: (BuildContext context, Widget? child) => CustomWidgetInspector(
        child: child ?? const SizedBox(),
      ),

      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class User {
  final int? max;

  User(this.max);
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    debugPrint('YM-----------------initState');
    WidgetsBinding.instance?.addObserver(this); //添加观察者

    // User a = User(100000);
    // User b  = User(100000);

    double a = 10000;
    double b = 10000;
    bool t = identical(a, b);

    debugPrint('----------t: $t-----${a == b}');
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    debugPrint('YM-----------------didChangeDependencies');
  }

  @override
  void didUpdateWidget(covariant MyHomePage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    debugPrint('YM-----------------didUpdateWidget');

    //identical
  }

  // @override
  // void deactivate() {
  //   bool a = ModalRoute?.of(context)?.isCurrent ?? false;
  //   if (a) {
  //     debugPrint('YM-----------------返回主页');
  //   }
  //
  //   debugPrint('YM-----------------返回主页aaa $a');
  // }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance?.removeObserver(this); //添加观察者
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        //  应用程序处于闲置状态并且没有收到用户的输入事件。
        //注意这个状态，在切换到后台时候会触发，所以流程应该是先冻结窗口，然后停止UI
        // print('YM----->AppLifecycleState.inactive');
        break;
      case AppLifecycleState.paused:
        //      应用程序处于不可见状态
        // print('YM----->AppLifecycleState.paused');
        break;
      case AppLifecycleState.resumed:
        //    进入应用时候不会触发该状态
        //  应用程序处于可见状态，并且可以响应用户的输入事件。它相当于 Android 中Activity的onResume。
        // print('YM----->AppLifecycleState.resumed');
        break;
      case AppLifecycleState.detached:
        //当前页面即将退出
        // print('YM----->AppLifecycleState.detached');
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('---------------------build');
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => const Test1()));
                },
                child: const Text("性能测试1")),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => const Test2()));
                },
                child: const Text("性能测试2")),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => const Test3Isolate()));
                },
                child: const Text("性能测试3--isolate")),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => const Test4Provider()));
                },
                child: const Text("测试4--provider")),
            ElevatedButton(
                onPressed: () {
                  // nestedOk5();
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => const Test5ConstKey()));
                },
                child: const Text("测试5--  Const Key")),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (_) => const Test6AnimationController()));
                },
                child: const Text("测试6--Slow Animations")),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (_) => Test7TimeLine()));
                },
                child: const Text("性能测试7--devtools")),
          ],
        ),
      ),
    );
  }

  int? foo, bar;

  void nestedOk5() {
    debugPrint('---------1');
    foo = 0;
    bar = 9;
    if (foo != null) {
      if (bar != null) {
        debugPrint('---------2');
        return;
      }
    }
    debugPrint('---------3');
    if (bar != null) {
      debugPrint('---------4');
    } // OK
  }
}

class ConsDemo extends StatefulWidget {
  const ConsDemo({Key? key}) : super(key: key);

  @override
  _ConsDemoState createState() => _ConsDemoState();
}

class _ConsDemoState extends State<ConsDemo> {
  // late bool _isShown;
  //
  // @override
  // void initState() {
  //   // TODO: implement initState
  //   super.initState();
  //   _isShown = true;
  // }

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Column(
      children: [
        // _isShown ? RaisedButton(onPressed: null) : Text("不显示"),
        Container(),
      ],
    ));
  }
}
